def binary_search_recursive(nums, target) -> int:
    def recursive_search(left, right):
        # 边界条件：查找区间无效时返回-1
        if left > right:
            return -1
        mid = (left + right) // 2
        # 找到目标值，返回索引
        if nums[mid] == target:
            return mid
        # 目标值在右半部分，递归查找右半部分
        elif nums[mid] < target:
            return recursive_search(mid + 1, right)
        # 目标值在左半部分，递归查找左半部分
        else:
            return recursive_search(left, mid - 1)

    # 初始调用递归函数，查找整个数组
    return recursive_search(0, len(nums) - 1)

# 示例用法
if __name__ == "__main__":
    # 测试数据（必须是有序的）
    data = [1, 2, 3, 5, 8, 9, 12]
    target1 = 8
    target2 = 7

    # 测试查找目标值8
    result1 = binary_search_recursive(data, target1)
    print(f"目标值 {target1} 在索引 {result1} 处找到")
